<%@page import="multiface.web.util.categories.AlberoCategorieRicambi"%>
<%@page import="multiface.awwadco.jpa.Utente"%>
<%@page import="multiface.web.util.PagerList"%>
<%@page import="javax.persistence.Query"%>
<%@page import="java.util.LinkedList"%>
<%
    Utente ut = (Utente) session.getAttribute("admin");
    if (ut == null) {
        response.sendRedirect(multiface.web.util.ServletUtils.getBaseUrl(request) + "/admin/index.jsp?err=y");
    }
%>

<%@page import="java.util.Iterator"%>
<%@page import="multiface.awwadco.jpa.Categoria"%>
<%@page import="java.util.List"%>
<%@page import="javax.persistence.RollbackException"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<script type="text/javascript">
    $(document).ready(function(){
        $("#formBox").validate();
    }); 
</script>

<div class="box">
    <form class="formBox" id="formBox" method="post" action="">
        <div class="title">Add New Category</div>
        <br>
        
        <div class="inputs">
            <label for="nome">Name of the Category</label>
            <input name="nome" id="nome" maxlength="255" class="required" type="text"/>
        </div>

        <input name="ordine" id="ordine" hidden="true" type="text" value="0"/>

        <input name="ricambio" id="ricambio" hidden="true" type="checkbox" value="1"/>
        <input name="sopracategoria" id="sopracategoria" type="hidden" value="-1"/>
    </form>
</div>

<%
    AlberoCategorieRicambi tree = new AlberoCategorieRicambi();
    tree.caricaDaDB();
%>
<div class="box">
    <div class="title">Super Category</div>
    <ul class="ul_open">
        <%
            List<Categoria> lcat = tree.categorieInOrdineDiProfondita();
            Iterator it = lcat.iterator();
            int sc = -1;
            int csc = 0;
            int p = 0;
            int[] arrpos = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

            while (it.hasNext()) {
                Categoria c = (Categoria) it.next();
                if (c.getSopracategoria() > sc) {
                    // entro in una sottocategoria
                    csc++;
                    p++;
                    sc = c.getSopracategoria();
                    //
                    out.println("<ul id=\"sottoli_" + sc + "\" class=\"ul_close\">");
                }
                if (c.getSopracategoria() < sc) {
                    //esco da una sottocategoira
                    int h = tree.Altezza(c);
                    //Sy_stem.out.println("[h=" + h + "]");
                    int m = (p - h) + 1;
                    for (int i = 0; i < m; i++) {
                        arrpos[p] = 0;
                        p--;
                        sc = c.getSopracategoria();
                        out.println("</ul>");
                    }
                }
                arrpos[p] = arrpos[p] + 1;
                String pre = "";
                for (int i = 0; i < p + 1; i++) {
                    pre += (arrpos[i] + "");
                    if (i < p) {
                        pre += ".";
                    }
                }
                out.println("<li class=\"li-cat\" id=\"li_cat_" + c.getIdcategoria() + "\"><a href=\"javascript:set_supercat(" + c.getIdcategoria() + ");\">" + pre + " - " + c.getNome() + "</a></li>");
            }
        %>
    </ul>
</div>